package com.sk.bilibili.view.fragment.main.home.shop;

import android.content.Context;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.sk.bilibili.adapter.vlayout.main.home.shop.BaseGoodsAdapter;
import com.sk.bilibili.bean.Goods;
import com.sk.bilibili.model.fragment.main.shop.GoodsModel;
import com.sk.bilibili.view.fragment.BaseRefreshFragment;

import java.lang.reflect.Constructor;
import java.util.List;

import lombok.SneakyThrows;

public class GoodsFragment extends BaseRefreshFragment<GoodsModel> {

    private BaseGoodsAdapter<?> baseGoodsAdapter;
    private boolean magic;
    private Constructor<? extends BaseGoodsAdapter<?>> constructor;

    @SneakyThrows
    public GoodsFragment(Class<? extends BaseGoodsAdapter<?>> clazz, boolean magic) {
        constructor = clazz.getConstructor(Context.class);
        this.magic = magic;
    }

    @SneakyThrows
    @Override
    protected void initAdapter(DelegateAdapter adapter) {
        baseGoodsAdapter = constructor.newInstance(getContext());
        adapter.addAdapter(baseGoodsAdapter);
    }

    @Override
    public void onBottom() {
        model.loadData(magic);
    }

    @Override
    public void onRefresh() {
        model.refreshData(magic);
    }

    public void loadDataSuccess(List<Goods> records) {
        if(records.isEmpty()){
            toast("没有更多了");
        }else {
            baseGoodsAdapter.addData(records);
        }
    }

    public void refreshDataSuccess(List<Goods> records) {
        baseGoodsAdapter.setData(records);
        cancelRefresh();
    }

}